package com.htu.edu.service.impl;

import com.htu.edu.bean.Business;
import com.htu.edu.bean.Cart;
import com.htu.edu.bean.Food;
import com.htu.edu.mapper.BusinessMapper;
import com.htu.edu.mapper.CartMapper;
import com.htu.edu.mapper.FoodMapper;
import com.htu.edu.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.dc.pr.PRError;

import java.util.List;
@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private FoodMapper foodMapper;
    @Autowired
    private BusinessMapper businessMapper;
    @Override
    public List<Cart> listCart(Cart cart) throws Exception {
        List<Cart> cartList = cartMapper.listCart(cart);
        for (Cart c : cartList) {
            Food food = foodMapper.getFoodById(c.getFoodId());
            if (food != null) {
                c.setFood(food);
            }
            Business business = businessMapper.getBusinessById(cart.getBusinessId());
            if (business != null) {
                c.setBusiness(business);
            }
        }
        return cartList;
    }

    @Override
    public int saveCart(Cart cart) throws Exception {
        cart.setQuantity(1);//默认数量为1
        int i = cartMapper.saveCart(cart);
        return i;
    }

    @Override
    public int removeCart(Cart cart) throws Exception {
        int i = cartMapper.removeCart(cart);
        return i;
    }

    @Override
    public int updateCart(Cart cart) throws Exception {
        int i = cartMapper.updateCart(cart);
        return i;
    }
}
